﻿using BLL;
using IBLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using user_defined_Panl;
using WindowsFormsApp1.登录老师账号界面;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WindowsFormsApp1
{
  
    public partial class   FrAddExam : Form
    {
        IFrStudentManage_IBLL frStudent =new FrStudentManage_BLL();
        IFrQuestionBankManage_IBLL frQuestion = new FrQuestionBankManage_BLL();
        public static FlowLayoutPanel[] layoutPanel=new FlowLayoutPanel[4];
   
        public FrAddExam()
        {
            InitializeComponent();
            s();
        }
        //初始化
        public void s()
        {
            layoutPanel[0] = flowLayoutPanel1;
            layoutPanel[1] = flowLayoutPanel2;
            layoutPanel[2] = flowLayoutPanel3;
            layoutPanel[3] = flowLayoutPanel4;
            uiComboBox1.DataSource= frStudent.SCourse_info();
            uiComboBox1.DisplayMember = "CourseName";
            uiComboBox1.ValueMember = "CourseId";
            uiComboBox2.DataSource= frStudent.SClass();
            uiComboBox2.DisplayMember = "ClassName";
            uiComboBox2.ValueMember = "ClassId";
        }
       

        private void button1_Click(object sender, EventArgs e)
        {
            //if (flowLayoutPanel1.Controls.Count*2+flowLayoutPanel2.Controls.Count*2+flowLayoutPanel3.Controls.Count*4+flowLayoutPanel4.Controls.Count*10<100)
            //{

            //    MessageBox.Show("添加失败,总分不够100");
            //    return;
            //}
            if (uiTextBox1.Text.Trim()==null)
            {
                MessageBox.Show("试卷名称不能为空");
                return;
            }
            if (uiTextBox2.Text.Trim()==null)
            {
                MessageBox.Show("考试时长不能为空");
                return;
            }
            if (!int.TryParse(uiTextBox2.Text.Trim(),out int d) )
            {
                MessageBox.Show("考试时长格式不正确");
                return;
            }
            string id = $"insert into Exam_info(PapersName,TestStartTime,TestDuration,ClassId,CourseId)  values('{uiTextBox1.Text}','{uiDatetimePicker1.Value}','{uiTextBox2.Text}','{uiComboBox1.SelectedValue}','{uiComboBox2.SelectedValue}')  SELECT @@IDENTITY";
             id=  frQuestion.Query_ID(id).ToString();
            //id = "8";
           StringBuilder builder = new StringBuilder();
            foreach (FlowLayoutPanel item in flowLayoutPanel1.Controls)
            {
                builder.Append($"insert into SingleChoice_Exam (Exam_id,SCID) values({id},{item.Controls[0].Tag}); ");
            }
            foreach (FlowLayoutPanel item in flowLayoutPanel2.Controls)
            {
                builder.Append($"insert into MultipleChoice_Exam (Exam_id,MCID) values({id},{item.Controls[0].Tag}); ");
            }
            foreach (FlowLayoutPanel item in flowLayoutPanel3.Controls)
            {
                builder.Append($"insert into TrueOrFalseQuestions_Exam (Exam_id,TOFID) values({id},{item.Controls[0].Tag}); ");
            }
            foreach (FlowLayoutPanel item in flowLayoutPanel4.Controls)
            {
                 builder.Append($"insert into Completion_Exam (Exam_id,CPID) values( {id} , {item.Controls[0].Tag}); ");
            }
            try
            {
                frQuestion.Query_ID(builder.ToString()).ToString();
                DialogResult dialog=  MessageBox.Show("试卷添加成功,是否继续添加","温馨提示",MessageBoxButtons.OKCancel);
                if (dialog==DialogResult.OK)
                {
                    uiButton2_Click(null, null);
                }
                else
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("试卷添加失败 错误信息:" +ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FrmQuestionBank frmQuestion=new FrmQuestionBank();
            frmQuestion.ShowDialog();
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            flowLayoutPanel1.Controls.Clear();
            flowLayoutPanel2.Controls.Clear();
            flowLayoutPanel3.Controls.Clear();
            flowLayoutPanel4.Controls.Clear();
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
           
            int a = flowLayoutPanel1.Controls.Count;
            for (int i = 0; i < a; i++)
            {

                if ((flowLayoutPanel1.Controls[i].Controls[0] as CheckBox).Checked == true)
                {
                    layoutPanel[0].Controls.RemoveAt(i);
                    i--;
                    a--;

                }
            }
            a = flowLayoutPanel2.Controls.Count;
            for (int i = 0; i < a; i++)
            {

                if ((flowLayoutPanel2.Controls[i].Controls[0] as CheckBox).Checked == true)
                {
                    layoutPanel[1].Controls.RemoveAt(i);
                    i--;
                    a--;

                }
            }
            a = flowLayoutPanel3.Controls.Count;
            for (int i = 0; i < a; i++)
            {

                if ((flowLayoutPanel3.Controls[i].Controls[0] as CheckBox).Checked == true)
                {
                    layoutPanel[2].Controls.RemoveAt(i);
                    i--;
                    a--;

                }
            }
            a = flowLayoutPanel4.Controls.Count;
            for (int i = 0; i < a; i++)
            {

                if ((flowLayoutPanel4.Controls[i].Controls[0] as CheckBox).Checked == true)
                {
                    layoutPanel[3].Controls.RemoveAt(i);
                    i--;
                    a--;

                }
            }

        }
    }
}
